package buildings.threads;

import buildings.Floor;

public class Semaphore {

    Floor floor;
    boolean canClean = false;

    public Semaphore(Floor floor) {
        this.floor = floor;
    }

    public float repair(int current) throws InterruptedException {
        float square;
        synchronized (floor) {
            while (canClean) {
                floor.wait();
            }
            square = floor.getSpaceByNumber(current).getSquare();
            System.out.println("Repairing space number "
                    + current + " with total area "
                    + square
                    + " square meters");
            canClean = true;
            floor.notifyAll();
        }
        return square;
    }

    public float clean(int current) throws InterruptedException {
        float square;
        synchronized (floor) {
            while (!canClean) {
                floor.wait();
            }
            square = floor.getSpaceByNumber(current).getSquare();
            System.out.println("Cleaning room number "
                    + current + " with total area "
                    + square
                    + " square meters");
            canClean = false;
            floor.notifyAll();
        }
        return square;
    }

    public Floor getFloor() {
        return floor;
    }
}
